import { getCommentList, getPageViews } from '@/services';
import { ICommentItem, IPageItem } from '@/types';
import { Effect, Reducer } from 'umi';

export interface PageModelState {
    pageItem:IPageItem;
    pageCommentList:ICommentItem[];
    pageCommentLength:number;
}

export interface PageModelType {
  namespace: 'page';
  state: PageModelState;
  effects: {
    getPageViews:Effect;
    getPageComment:Effect
  };
  reducers: {
    save: Reducer<PageModelState>;
  };
}

const PageModel: PageModelType = {
  namespace: 'page',

  state: {
    pageItem:{} as IPageItem,
    pageCommentList:[],
    pageCommentLength:0
  },

  effects: {
    *getPageViews({payload},{call,put}){
        const result=yield call(getPageViews,payload)
        if(result.data){
            yield put({
              type:'save',
              payload:{
                pageItem:result.data,
              }
            })
        }
    },
    *getPageComment({payload},{call,put}){
        const result=yield call(getCommentList,payload)
        if(result.data){
            yield put({
              type:'save',
              payload:{
                pageCommentList:result.data[0],
                pageCommentLength:result.data[1]
              }
            })
        }
    }
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default PageModel;